<template>
  <el-form ref="form" :model="form" label-width="80px" style="padding: 20px;">
    <el-form-item label="发送人">
      <el-input v-model="form.sender" disabled></el-input>
    </el-form-item>
    <el-form-item label="接收人">
      <el-input v-model="form.receiver" disabled></el-input>
    </el-form-item>
    <el-form-item label="内容">
      <el-input v-model="form.content" type="textarea"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">发送</el-button>
      <el-button @click="resetForm">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: "MessageForm",
  props: {
    sender: String,
    receiver: String,
  },
  data() {
    return {
      form: {
        sender: this.sender,
        receiver: this.receiver,
        content: "",
      },
    };
  },
  methods: {
    clearForm() {
      console.log("clearForm method called");
      this.form.content = "";
      this.$refs.form.resetFields();
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit("submit", this.form);
          this.resetForm();
        } else {
          console.log("表单验证失败！");
        }
      });
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
  },
};
</script>